#include <QFileInfo>
#include <QDebug>
#include <QList>
#include "MYSubtitle.h"

MYSubtitle::MYSubtitle()
{

}

MYSubtitle::~MYSubtitle()
{

}

// 一句字幕信息， 包含编号，起始时间，结束时间和文字
typedef struct subNode {
    int num;
    int beginTime;  // 单位是ms
    int endTime;    // 单位是ms
    QString text;
}subNode;

QList<subNode> subNodeList;

// 打开字幕文件
void MYSubtitle::Open(QString path)
{
    m_SubtitleFile.reset(new QFile(path));
    QFileInfo fileInfo(path);
    if(fileInfo.suffix() == "srt")
        m_SubtitleType = SUBTITLE_TYPE_SRT;
    // 只读，读取时行末终结符被替换为'\n'
    if(m_SubtitleFile->open(QIODevice::ReadOnly)) {
        qDebug() << "open subtitle success";
        subNodeList.clear();
        m_isOpen = true;
        // 循环读取文件加载字幕
        // srt文件的格式是 每一句字幕 = 一行编号+一行时间+字幕（可能不止一行）
        // 字幕之间会有一行空行隔开
        /*
        1
        00:00:01,410 --> 00:00:04,740
        这小狐狸是谁呀 这是泰勒先生的情人
        */
        while(!m_SubtitleFile->atEnd()) {
            subNode sn;
            QString line = "";
            QString cache = "";

            // 读取编号
            line = (QString)m_SubtitleFile->readLine();
            // 去除结尾的结束符, 从左侧开始截取
            sn.num = line.left(line.length() - 2).toInt();

            // 读取时间
            line = (QString)m_SubtitleFile->readLine();

            // mid 函数， 从某个位置开始，截取长度为n的子字符串
            int begin_hour = line.mid(0,2).toInt();
            int begin_minute = line.mid(3,2).toInt();
            int begin_second = line.mid(6,2).toInt();
            int begin_milli = line.mid(9,3).toInt();
            int beginTime = (begin_hour * 3600 + begin_minute * 60 + begin_second) * 1000 + begin_milli;  //起始时间
            int end_hour = line.mid(17,2).toInt();
            int end_minute = line.mid(20,2).toInt();
            int end_second = line.mid(23,2).toInt();
            int end_milli = line.mid(26,3).toInt();
            int endTime = (end_hour * 3600 + end_minute * 60 + end_second) * 1000 + end_milli;            //终止时间

            sn.beginTime = beginTime;
            sn.endTime = endTime;

            // 读取字幕, 字幕可能不止一行
            line = (QString)m_SubtitleFile->readLine();
            while(line != "\r\n" && line != "\n") {
                int idx = line.indexOf('{');
                while(idx != -1) {
                    line.replace(idx, line.indexOf("}") - idx + 1, "");
                }
                cache += line.left(line.length() - 2) + "\n";        //去除"\r\n",添加"\n"
                line = (QString)m_SubtitleFile->readLine();
            }
            cache = cache.left(cache.length() - 1); // 去除结尾的'\n'

            sn.text = cache;
            subNodeList.append(sn);
        }
    }
    qDebug() << "subtitle load success!";
	return;
}

// 按照当前时间来读取字幕
bool MYSubtitle::Read(int currentTime)
{
    if(!m_SubtitleFile->isOpen()) return false;
    for(int i = 0; i < subNodeList.count(); ++i) {
        if(subNodeList[i].beginTime < currentTime && subNodeList[i].endTime > currentTime) {
            m_NodeStartTime = subNodeList[i].beginTime;
            m_NodeEndTime = subNodeList[i].endTime;
            m_Text = subNodeList[i].text;
            return true;
        }
        if(subNodeList[i].beginTime > currentTime) {
            return false;
        }
    }
    return false;
}

// 是否打开
bool MYSubtitle::isOpen()
{
    return m_isOpen;
}
//开始时间
int MYSubtitle::nodeStartTime()
{
    return m_NodeStartTime;
}
//结束时间
int MYSubtitle::nodeEndTime()
{
    return m_NodeEndTime;
}
//字幕内容
QString MYSubtitle::text()
{
    return m_Text;
}

